/***************************************************************************
 *   oboe.math.Number2D                                                    *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.math
{
	public class Number2D
	{
		public var x : Number;
		public var y : Number;
		public function Number2D( x:Number=0, y:Number=0 )
		{
			this.x = x;
			this.y = y;
		}
		public function set( x : Number, y : Number ) : Number2D
		{
			this.x = x;
			this.y = y;
			return this;
		}
		public function clone() : Number2D
		{
			return new Number2D( x, y );
		}
		public function isEqual( o : Number2D ) : Boolean
		{
			return this.x == o.x && this.y == o.y;
		}
		public function normalize() : Number2D {
			var l : Number = Math.sqrt(x*x+y*y);
			x /= l;
			y /= l;
			return this;
		}
		public function length() : Number {
			return Math.sqrt(x*x+y*y);
		}
		public function lengthSquared() : Number {
			return x*x+y*y;
		}
		public function mul( f : Number ) : Number2D {
			x *= f;
			y *= f;
			return this;
		} 
		public function sub( s : Number ) : Number2D {
			x -= s;
			y -= s;
			return this;
		}
		public function sub3( a : Number, b : Number ) : Number2D {
			x -= a;
			y -= b;
			return this;
		}
		public function sub2( s : Number2D ) : Number2D {
			x -= s.x;
			y -= s.y;
			return this;
		}
		public function add( n : Number ) : Number2D {
			x += n;
			y += n;
			return this;
		}
		public function add2( n : Number2D ) : Number2D {
			x += n.x;
			y += n.y;
			return this;
		}
		public function mul2( n : Number2D ) : Number2D {
			x *= n.x;
			y *= n.y;
			return this;
		}
		public function addRandom( min : Number, max : Number ) : Number2D {
			x += min + Math.random()*(max-min);
			y += min + Math.random()*(max-min);
			return this;
		}
		public function mulRandom( min : Number, max : Number ) : Number2D {
			x *= min + Math.random()*(max-min);
			y *= min + Math.random()*(max-min);
			return this;
		}
		public function copyTo( n : Number2D ) : Number2D {
			n.x = x;
			n.y = y;
			return n;
		}
		public function copyFrom( n : Object ) : Number2D {
			x = n.x;
			y = n.y;
			return this;
		}
		public function round() : Number2D {
			x = int(x+0.5);
			y = int(y+0.5);
			return this;
		}
		public function floor() : Number2D {
			x = int(x);
			y = int(y);
			return this;
		}
		public function flip() : Number2D {
			var t : Number = y;
			y = x;
			x = t;
			return this;
		}
		public function toString() : String {
			return "{ x: " + x + ", y: " + y + " }"; 
		}
	}
}